<?php 
/**
 * Basketball extension for Joomleague
 * @version $Id: controller.php 95 2008-02-22 21:04:45Z julien.vonthron $
 * @package JlgBasketballExtension
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgBasketballExtension is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */
 
include_once('classes/BasketballProject.php');

// change the type of $joomleague to use extended functions
  $joomleague = new BasketballProject( $database);
  $joomleague->load($p);

switch($jl_func){
	//remember to set $jl_func to a dummy value if you don't want to trigger 
	//matching value in default switch
	
  case "showplayersranking";
    require('models'.'/'.'playersranking.php');
    showPlayersRanking();
    $jl_func = "end";
    break;
    
  case "showreport";
    $match_id = mosGetParam($_REQUEST,'mid', 0);
    $sql="SELECT summary FROM #__joomleague_matches where match_id=$match_id";
		$database->setQuery($sql);
		if ($res = $database->loadResult())
    echo "match summary: $res";
    $jl_func = "end";
    break;
    
  case "showplayer":
    // pid is player id not playertool id
    $showPlayer_config=$joomleague->getTemplateConfig("show_player.tpl");
    if($pid > 0) {
      $history = jl_getPlayerHistory($pid, 'DESC');
      $historyStaff = jl_getStaffHistory ($pid, 'DESC');
      
      $query = "SELECT p.*, 
                       pt.id AS playertoolid, pt.*, 
                       pp.name as position, pp.isStaff, 
                       proj.name as proj_name, 
                       t.name as team_name
                FROM #__joomleague_playertool pt
                INNER JOIN #__joomleague_team_joomleague tt ON (pt.team_id = tt.team_id AND tt.project_id=pt.project_id) 
                INNER JOIN #__joomleague_players p ON p.id = pt.player_id
                INNER JOIN #__joomleague_teams AS t ON t.id = pt.team_id
                INNER JOIN #__joomleague_positions pp ON pp.id = pt.position_id
                INNER JOIN #__joomleague AS proj ON pt.project_id = proj.id
                INNER JOIN #__joomleague_seasons AS s ON s.id = proj.season_id
                INNER JOIN #__joomleague_leagues AS l ON l.id = proj.league_id
                WHERE p.id = $pid AND proj.published=1
                ORDER BY s.ordering DESC, l.ordering DESC, proj.name ASC";
      $database->setQuery($query);
      $rows = $database->loadObjectList();
      
      if(count($rows) > 0)
      {
        // find row of current project
        foreach ($rows as $k => $r) 
        {
        	if ($r->project_id == $joomleague->id) 
          {
        		$row=&$rows[$k];
        		break;
        	}
        }
        
        // get contact_id if needed
        if ( $showPlayer_config['show_user_profile'] == 1 ) 
        {
             $query = "SELECT id FROM #__contact_details WHERE user_id = $row->jl_user_id AND catid=".$showPlayer_config['contact_cat_id'];
             $database->setQuery($query);
             if (!$contact_id = $database->loadResult()) {
               //echo $database->getQuery();
             }
        }
        $eventtypes = $joomleague->getPositionEventTypes($row->position_id);
               
        $mainframe->setPageTitle(_JL_PAGE_TITLE_PLAYER.' - '.$row->firstname.' '.$row->lastname);
        require(getTemplateFile(JL_TEMPLATE_PATH, 'show_player.tpl.php'));
      }
      else print "no data";
    }
    $jl_func = "end";
    break;
    
  case "showplayers":
  
    $showPlayers_config=$joomleague->getTemplateConfig("show_players.tpl");
    
    if(is_a($teamtool,'Teamtool'))
    {
      $team = new JL_Team( $database );
      $team->load($teamtool->team_id);
      $mainframe->setPageTitle(_JL_PAGE_TITLE_PLAYERS.' - '.$team->name);
      
      #list players of a team
      $query = "SELECT pt.*, p.*, p.id AS pid, pos.name as position
                          FROM #__joomleague_playertool pt
                          INNER JOIN #__joomleague_players p ON pt.player_id = p.id
                          INNER JOIN #__joomleague_positions pos ON pos.id = pt.position_id
                          WHERE pt.team_id = '$team->id' AND
                          pt.project_id = '$teamtool->project_id'
                          ORDER BY pos.ordering, pt.position_id, pt.position_number";
      $database->setQuery($query);
      $rows = $database->loadObjectList();
      //print_r($rows); exit;

      if ($showPlayers_config['show_stats']==1&&count($rows)>0) 
      {
        $PosEventtypes=$joomleague->getPositionEventTypes();
        foreach ($rows AS $row) 
        {
          $playerEvents[$row->id] = array();
          if ( isset($PosEventtypes[$row->position_id]) ) 
          {
            foreach ($PosEventtypes[$row->position_id] as $event) {             
              $playerStats[$row->id][$event->eventtype_id] = $joomleague->getPlayerEvents($row->player_id, $event->eventtype_id, $row->team_id);
            }
          }
        }
        //print_r($playerStats);exit;
      }
      
      $query = sprintf("SELECT p.*, pos.name as position, ts.picture
                          FROM #__joomleague_teamstaff_project ts
                          INNER JOIN #__joomleague_players p ON ts.person_id = p.id
                          INNER JOIN #__joomleague_positions pos ON pos.id = ts.position_id
                          WHERE projectteam_id= '%s'     
                          ORDER BY pos.ordering, ts.position_id
                      ", $teamtool->id);
      $database->setQuery($query);
      $stafflist = $database->loadObjectList();   
      require(getTemplateFile(JL_TEMPLATE_PATH, 'show_players.tpl.php'));
    }
    else print "no data!";   
    $jl_func = "end";
    break;
    
  case "eventsstats":
    $limitstart     = 0;
		$events_config  = $joomleague->getTemplateConfig("show_events_stats.tpl");
		$eventtype      = intval(mosGetParam($_REQUEST, 'evid', 0));
		
		if ( $eventtype>0 ) 
    { // display full ranking for specified event stat
			$limit= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
			if (array_key_exists( 'max_events', $events_config ) 
          && intval( $events_config['max_events'] ) > 0 ) {
        $limit = $events_config['max_events'];
      }
      $limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );
		}
		else {
		  // display first rows of all stats events
			$limit = (isset($events_config['count_events'])) ? $events_config['count_events'] : 5;
		}
		
		require('models'.'/'.'eventsstats.php');
    $model = new BasketballModelEventsStats();
    // get the event type object, or all of events types from project if null
  	$eventtypes = $model->getEventTypes( $joomleague->id, $eventtype );
  	
  	// set page title
		$title=_JL_MODULE_NEXT_MATCH_STATISTIC." ";
		if ($eventtype>0) {
   		$title .= $eventtypes[$eventtype]->name;
  	}
  	else {
  		$title .= _JL_MODULE_NEXT_MATCH_STATISTIC.' - '._JL_SEF_EVENTS;
  	}
  	$mainframe->setPageTitle($title);

    // get the stats per event
  	if (array_keys($eventtypes))
    {
	  	foreach ( $eventtypes AS $eventkey => $value) {
				$eventrankings[$eventkey] = $model->getEventsRanking( $joomleague->id, $eventkey, $limit, $limitstart );
			}
		}
		require(getTemplateFile(JL_TEMPLATE_PATH, 'show_events_stats.tpl.php'));
		$jl_func = "end";
	break;
}

?>
